﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilsCache;
using UtilsORM;
using UtilsORM.MySqlData;

namespace ServiceWeb.DBGateway
{
    public class DBIotDeviceLiveStateCache
    {


       

        /// <summary>
        /// 缓存增加 获取变量列表
        /// </summary>
        /// <param name="cguid"></param>
        /// <returns></returns>
        public int getGateWayOnlineCount()
        {
            EFYihongIotContext ef = new EFYihongIotContext();
            int count = 0;
            List<iot_device> devices = (from d in ef.iot_device
                                        join m in ef.iot_device_model on d.d_model_guid equals m.m_guid
                                        where m.m_attribute == 3
                                        select d).ToList();
            foreach (var device in devices)
            {
                if (getGateWayIsOnlineByCache(device.d_guid) != null)
                {
                    count = count + 1;
                }
            }
            return count;
        }
        /// <summary>
        /// 缓存增加 获取变量列表
        /// </summary>
        /// <param name="cguid"></param>
        /// <returns></returns>
        public iot_device_live_state getGateWayIsOnlineByCache(string gwdguid)
        {
            iot_device_live_state model = UtilMemoryCacheHelper.StringGet<iot_device_live_state>("web_getGateWayIsOnlineByCache" + gwdguid);
            if (model == null)
            {
                EFYihongIotContext ef = new EFYihongIotContext();
                model = ef.iot_device_live_state.Where(c => c.ls_d_guid == gwdguid && c.ls_live_state_set_time > DateTime.Now.AddMinutes(-1)).FirstOrDefault();
                if (model != null)
                {
                    UtilMemoryCacheHelper.StringSet("web_getGateWayIsOnlineByCache" + gwdguid, model, 1);
                }
                ef.Dispose();
            }
            return model;
        }

        public bool getGateWayIsOnline(string gwdguid)
        {
            iot_device_live_state model = UtilMemoryCacheHelper.StringGet<iot_device_live_state>("web_getGateWayIsOnline" + gwdguid);
            if (model == null)
            {
                EFYihongIotContext ef = new EFYihongIotContext();
                model = ef.iot_device_live_state.Where(c => c.ls_d_guid == gwdguid && c.ls_live_state_set_time > DateTime.Now.AddMinutes(-1)).FirstOrDefault();
                if (model != null)
                {
                    UtilMemoryCacheHelper.StringSet("web_getGateWayIsOnline" + gwdguid, model, 1);
                    return true;
                }
                ef.Dispose();
                return false;
            }
            return true;
        }
        /// <summary>
        /// 产品中是否有网关离线
        /// </summary>
        /// <param name="pguid"></param>
        /// <returns></returns>
        public bool getProductGateWayIsOnlineByCache(string pguid)
        {
            EFYihongIotContext ef = new EFYihongIotContext();
            List<iot_device> devices = (from d in ef.iot_device
                                        join m in ef.iot_device_model on d.d_model_guid equals m.m_guid
                                        where m.m_attribute == 3 && d.d_p_guid == pguid && d.d_status == 0
                                        select d).ToList();
            foreach (var device in devices)
            {
                iot_device_live_state live_State = getGateWayIsOnlineByCache(device.d_guid);
                if (live_State == null)
                {
                    return false;
                }
            }
            return true;

        }
    }
}
